<?php
declare(strict_types=1);

namespace App\Dao;

use App\Model\Admin;

class AdminDao
{

    use \App\Lib\RbacTrait;
    
    //通过条件搜索
    public function searchByWhere($where = []){
        return Admin::query()->where($where)->get()->toArray();
        
    }

    //通过条件搜索单一条数据
    public function searchByWhereFirst($where){
        return Admin::query()->where($where)->first();
        
    }

    //通过条件搜索分页
    public function searchByWherePage($pageSize,$where = []){
        $data = Admin::where($where)->paginate(intval($pageSize),['*'],'page')->toArray();
        return $data['data'];
    }

    //通过条件统计数量
    public function countByWhere($where = []){
        return Admin::query()->where($where)->count();
        
    }
    //通过管理员id找角色
    public function manageRole($manage_id)
    {
        $role = static::roleListByUserid($manage_id);
        $role_name = array_column($role, 'name');
        return implode($role_name);
    }

    //通过管理员id找角色id
    public function manageRoleid($manage_id)
    {
        return static::roleIdByUserid($manage_id);
    }

    //
    public function add($data)
    {
        return Admin::create($data);
    }

    public function update($where,$data)
    {
        return Admin::query()->where($where)->update($data);
    }

    /**
     * 通过管理员名称查找id
     * @param $name
     * @return \Hyperf\Utils\Collection
     */
    public function searchIdsByName($name)
    {
        return Admin::query()->where('name','like' ,'%'.$name.'%')->pluck('id');
    }

    
}
